<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Setting extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
    }
    
    public function index()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->model('category/category_model');
        $this->load->model('setting/setting_model');
        if ($this->input->post()) {
            $lstMenubar = $this->input->post('lstMenubar');
            $lstBoxHome = $this->input->post('lstBoxHome');
			$lstColumnRight = $this->input->post('lstColumnRight');
            if ($this->setting_model->setOption('lstMenubar', $lstMenubar)) {
                if ($this->setting_model->setOption('lstBoxHome', $lstBoxHome)) {
                	if ($this->setting_model->setOption('lstColumnRight', $lstColumnRight)) {
                        $this->mycommon->resetCache('app/cache');
	                    echo "Cập nhật thành công!";
	                    die;
	                }
                    echo "Có lỗi xảy ra, vui lòng thử lại";
                	die;
                }
                echo "Có lỗi xảy ra, vui lòng thử lại";
                die;
            }
            echo "Có lỗi xảy ra, vui lòng thử lại";
            die;
        }
        $lstMenubar = $this->setting_model->getOptionByName('lstMenubar');
        $lstBoxHome = $this->setting_model->getOptionByName('lstBoxHome');
		
		$lstColumnRight = $this->setting_model->getOptionByName('lstColumnRight');
		
        $arrMenubar = ($lstMenubar) ? explode(',', $lstMenubar['value']) : '';
        $arrBoxHome = ($lstBoxHome) ? explode(',', $lstBoxHome['value']) : '';
		$arrColumnRight = ($lstColumnRight) ? explode(',', $lstColumnRight['value']) : '';
		
        $data['lstMenubar'] = '';
        $data['lstBoxHome'] = '';
		$data['lstColumnRight'] = '';
		
        if ($arrMenubar != '') {
            foreach ($arrMenubar as $k=>$v) {
                $data['lstMenubar'][$k]['info'] = $this->category_model->getCategoryById($v);
            }
        }
		
        if ($arrBoxHome != '') {
            foreach ($arrBoxHome as $_k=>$_v) {
                $data['lstBoxHome'][$_k]['info'] = $this->category_model->getCategoryById($_v);
            }
        }
		
		if ($arrColumnRight != '') {
            foreach ($arrColumnRight as $_k=>$_v) {
                $data['lstColumnRight'][$_k]['info'] = $this->category_model->getCategoryById($_v);
            }
        }
		
		$category = $this->category_model->getAllCategory();
		
		$listCategory = array();
		$listCategoryChild = array();
		foreach ($category as $key => $cat) {
			if($cat['parent_id']== 0) {
				$listCategory[] = $cat;
			} else {
				$listCategoryChild[] = $cat;
			}
		}
		foreach ($listCategory as $key => $value) {
			$listCategory[$key]['child'] = array();
			foreach ($listCategoryChild as $k => $val) {
				if($val['parent_id'] == $value['id']) {
					$listCategory[$key]['child'][] = $val;
				}
			}
		}
        $data['category'] = $listCategory;

        $this->admin_layout->view('setting/setting_view', $data);
    }

}

?>
